﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using SomeGame.GUI.Widgets.Events.Click;

namespace SomeGame.GUI.Widgets.InlineWidgets {

    public class Button : TextWidget, IClickPublisher {

        private readonly IList<IClickSubscriber> ClickSubscribers = new List<IClickSubscriber>();

        public Button() {
            // ...
        }

        public Button(Texture2D texture, Color color) : base(texture, color) {
            // ...
        }

        public Button(Texture2D texture, int width, int height, Color color) : base(texture, width, height, color) {
            // ...
        }

        public Button(Texture2D texture, Color color, SpriteFont font, Color fontColor, string text)
            : base(texture, texture.Width, texture.Height, color, font, fontColor, text) {
            // ...
        }

        public Button(Texture2D texture,
            int width,
            int height,
            Color color,
            SpriteFont font,
            Color fontColor,
            string text) : base(texture, width, height, color, font, fontColor, text) {
            // ...
        }

        public void AddSubscriber(IClickSubscriber clickSubscriber) {
            ClickSubscribers.Add(clickSubscriber);
        }

        public void RemoveSubscriber(IClickSubscriber clickSubscriber) {
            ClickSubscribers.Remove(clickSubscriber);
        }

        public void Click() {
            foreach (IClickSubscriber clickSubscriber in ClickSubscribers) {
                clickSubscriber.OnClick(new ClickEventArgs(this));
            }
        }

        public override void Update(GameTime gameTime) {
            if (!Screen.IsTopScreen) {
                return;
            }
            // Hover.
            if (HoverColor != null) {
                CurrentColor = Bounds.Contains(GameEngine.MousePoint) ? HoverColor.Value : NormalColor;
            }
            // Click.
            if (GameEngine.MouseState.LeftButton == ButtonState.Pressed &&
                GameEngine.PreviousMouseState.LeftButton == ButtonState.Released) {
                if (Bounds.Contains(GameEngine.MousePoint)) {
                    Click();
                }
            }
        }

        public override void Draw() {
            base.Draw();
            if (Text != null) {
                // TODO: could optimize this so it doesn't happen every frame (the measuring).
                var buttonCenter = new Vector2(Bounds.X+Bounds.Width/2f, Bounds.Y+Bounds.Height/2f);
                Vector2 textCenter = GameEngine.Consolas16.MeasureString(Text)/2;
                GameEngine.SpriteBatch.DrawString(GameEngine.Consolas16,
                    Text,
                    buttonCenter,
                    FontColor,
                    0,
                    textCenter,
                    1.0f,
                    SpriteEffects.None,
                    0f);
            }
        }

    }

}